<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入工作单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="学历" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择学历" clearable size="small">
          <el-option
            v-for="dict in educationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="专业" prop="major">
        <el-input
          v-model="queryParams.major"
          placeholder="请输入专业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="负责科室" prop="dutyoffices">-->
<!--        <el-select v-model="queryParams.dutyoffices" placeholder="请选择负责科室" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dutyofficesOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="负责设备" prop="dutydevice">-->
<!--        <el-input-->
<!--          v-model="queryParams.dutydevice"-->
<!--          placeholder="请输入负责设备"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="联系方式" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zyx:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zyx:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zyx:member:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zyx:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="工作单位" align="center" prop="unit" />
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="职称" align="center" prop="position" />
<!--      <el-table-column label="负责科室" align="center" prop="dutyoffices" :formatter="dutyofficesFormat" />-->
      <el-table-column label="负责设备" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleSrch(scope.row.id)"

          >负责设备</el-button>
         </template>
       </el-table-column>
      <el-table-column label="联系方式" align="center" prop="mobile" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="230">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zyx:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zyx:member:remove']"
          >删除</el-button>
		  <el-button
		    size="mini"
		    type="text"
		    icon="el-icon-edit"
		    @click="handlePrint(scope.row)"
		  >打印</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改成员基础信对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" maxlength="50"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" maxlength="20"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" maxlength="20"/>
        </el-form-item>
        <el-form-item label="工作单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入工作单位" maxlength="50"/>
        </el-form-item>
        <el-form-item label="学历" prop="education">
           <el-input v-model="form.education" placeholder="请输入学历" maxlength="50"/>
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" maxlength="50"/>
        </el-form-item>
        <el-form-item label="职称" prop="position">
          <el-input v-model="form.position" placeholder="请输入职称" maxlength="50"/>
        </el-form-item>
        <el-form-item label="附录">
          <fileUpload  v-model="form.appendix"/>
        </el-form-item>
        <el-form-item label="工作简历" prop="resume">
          <el-input v-model="form.resume" type="textarea" placeholder="请输入工作简历" maxlength="500"/>
        </el-form-item>

        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="500"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"  v-preventReClick="3000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <member-device-srch-list ref="memberDeviceSrchListRef"></member-device-srch-list>
    <select-device-list ref="selectDeviceListRef"></select-device-list>
    <member-print ref="memberPrint" ></member-print>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember } from "@/api/zyx/member";
import FileUpload from '@/components/FileUpload';
import { treeselect as deviceTreeselect,  } from "@/api/zyx/device";
import { listTreeData,  } from "@/api/system/dept";
import { memberDeviceTreeselect,  } from "@/api/zyx/memberDevice";
import memberDeviceSrchList from '../memberDevice/memberDeviceSrchList.vue'
import selectDeviceList from '../device/selectDeviceList.vue'
import memberPrint from './memberPrint.vue'


export default {
  name: "Member",
  components: {
    FileUpload,
    memberDeviceSrchList,
    selectDeviceList,
    memberPrint,
  },
  data() {
    return {
      props: {
        label: 'label',
        children: 'zones',
        isLeaf: 'leaf'
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      checkedIds:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成员基础信表格数据
      memberList: [],
      expandedKeys:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 学历字典
      educationOptions: [],
      // 负责科室字典
      dutyofficesOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        unit: null,
        education: null,
        major: null,
        dutyoffices: null,
        dutydevice: null,
        mobile: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },

        ],
        mobile: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],

      },

      // 设备列表
      deviceOptions: [],
    };
  },
  created() {
    this.getList();
    // this.getDicts("zyx_education").then(response => {
    //   this.educationOptions = response.data;
    // });
    this.getDicts("zyx_dutyoffices").then(response => {
      this.dutyofficesOptions = response.data;
    });
  },
  methods: {
    //打印
    handlePrint(row){
       this.$refs.memberPrint.member=row
      this.$refs.memberPrint.visible = true;
    },
    /** 查询成员基础信列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // // 学历字典翻译
    // educationFormat(row, column) {
    //   return this.selectDictLabel(this.educationOptions, row.education);
    // },
    // 负责科室字典翻译
    dutyofficesFormat(row, column) {
      return this.selectDictLabel(this.dutyofficesOptions, row.dutyoffices);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        departid: null,
        name: null,
        age: null,
        unit: null,
        education: null,
        major: null,
        position: null,
        dutyoffices: null,
        dutydevice: null,
        dutydevices:null,
        mobile: null,
        appendix: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        resume:null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成员基础信";
    },
    /** 根据员工ID查询设备树结构 */
    getMemberDeviceTreeselect(memberid) {
      return memberDeviceTreeselect(memberid).then(response => {

        // this.menuOptions = response.menus;
        return response;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;


        this.title = "修改成员基础信";
      });
    },

    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // this.form.dutydevices = this.$refs.tree.getCheckedNodes();
            updateMember(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.dutydevices = this.$refs.tree.getCheckedNodes();
            addMember(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除成员基础?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有成员基础信数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    handleSrch(memberid){
      this.$refs.memberDeviceSrchListRef.memid=memberid
       this.$refs.memberDeviceSrchListRef.getList();
      this.$refs.memberDeviceSrchListRef.visible = true;
    },
    handleShowAdd(memberid){
       this.$refs.selectDeviceListRef.memberid=memberid
       this.$refs.selectDeviceListRef.visible = true;
    },
    handleHideAdd(){
       this.$refs.selectDeviceListRef.visible = false;
    }
  }
};
</script>
